package com.haven123.cloudapp.rabbitmq;


import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;


@Log4j2
@Configuration
public class RabbitConfig {
    @Autowired private RabbitTemplate rabbitTemplate;
    @PostConstruct
    public void setRabbitTemplate(){
        rabbitTemplate.setConfirmCallback(confirmCallback());
        rabbitTemplate.setReturnCallback(returnCallback());
    }
    @Bean
    public RabbitTemplate.ConfirmCallback confirmCallback(){
        return new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String caus) {
                log.info("发送方确认发消息 ack:{}",ack);
            }
        };
    }
    @Bean
    public RabbitTemplate.ReturnCallback returnCallback(){
        return new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String caus, String s1, String s2) {
                log.info("发送方失败：消息是{}",caus);
            }
        };
    }
}
